// import PushNotificationIOS from "@react-native-community/push-notification-ios";
import PushNotification from "react-native-push-notification";
import { Platform } from "react-native";

export const defaultConfig = {
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function (token) {
    console.log("TOKEN:", token);
  },

  // (required) Called when a remote is received or opened, or local notification is opened
  onNotification: function (notification) {
    console.log("NOTIFICATION:", notification);

    //处理通知
    //（必需）在接收或打开远程或打开本地通知时调用

    //notification.fining（PushNotificationIOS.FetchResult.NoData）；
  },

  //（可选）当按下Registered Action且invokeApp为false时调用，如果为true则调用onNotification（Android）
  onAction: function (notification) {
    console.log("ACTION:", notification.action);

    // process the action
  },

  //（可选）当用户注册远程通知失败时调用。通常发生在APNS出现问题时，或者设备是模拟器时。（iOS系统）
  onRegistrationError: function(err) {
    console.error(err.message, err);
  },

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  //是否应自动弹出初始通知
  //默认值：true
  popInitialNotification: true,

  /**
  *（可选）默认值：true
  *-指定是否会请求权限（ios）和令牌（android和ios），
  *-如果没有，则必须稍后调用PushNotificationsHandler.requestPermissions（）
  *-如果您没有使用远程通知或没有安装Firebase，请使用以下方法：
  *requestPermissions:Platform.OS===“ios”
  */
  requestPermissions: Platform.OS === 'ios'
}

export const setConfig = (config = defaultConfig) => {
  PushNotification.configure(config)
}

/**
 * 方法转发 详细文档参考 > https://github.com/zo0r/react-native-push-notification
 * @param {方法名} fucName 
 * @param  {参数} args 
 */
export const methodDispatch = (fucName, ...args) => {
  if (typeof (PushNotification[fucName]) == 'function') {
    PushNotification[fucName](...args)
  }
}